from twisted.words.protocols import irc
from twisted.internet import protocol

class MomBot(irc.IRCClient):
	def _get_nickname(self):
		return self.factory.nickname
	
	nickname = property(_get_nickname)
	
	def signedOn(self):
		self.join(self.factory.channel)
		print "Signed on as %s." % (self.nickname,)
	
	def joined(self, channel):
		print "Joined %s." % (channel,)
	
	def privmsg(self, user, channel, msg):
		if not user:
			print "Console: ", msg
			return
		if self.nickname in msg:
			msg = re.compile(self.nickname + "[:,]* ?", re.I).sub('', msg)
			prefix = "%s: " % (user.split('!', 1)[0], )
		else:
			prefix = ''
		print channel, user, prefix, msg
		'''
			self.msg(self.factory.channel, prefix + sentence)
		'''

class MomBotFactory(protocol.ClientFactory):
	protocol = MomBot
	
	def __init__(self, channel, nickname='listenner'):
		self.channel = channel
		self.nickname = nickname
		
	def clientConnectionLost(self, connector, reason):
		print "Lost connection (%s), reconnecting." % (reason,)
		connector.connect()
		
	def clientConnectionFailed(self, connector, reason):
		print "Could not connect: %s" % (reason,)

import sys
from twisted.internet import reactor

if __name__ == "__main__":
	chan = sys.argv[1]
	nick = sys.argv[2]
	reactor.connectTCP('irc.freenode.net', 6667, MomBotFactory('#' + chan, nick))
	reactor.run()

